// components/hg-editor/hg-editor.js



Component({
  /**
   * 组件的属性列表
   */
  properties: {
    /**是否显示工具栏 */
    showTabBar: {
      type: 'Boolean',
      value: true
    },
    placeholder: {
      type: 'String',
      value: '请输入相关内容'
    },
    name: {
      type: 'String',
      value: ''
    },
    uploadImageURL: {
      type: 'String',
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    addItalic_select:false,
    addBold_select:false,
    fileList:[0],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _onEditorReady: function () {
      const that = this;
      that.createSelectorQuery().select('#editor').context(function (res) {
        that.editorCtx = res.context
      }).exec()
    },

    //传出图片url
    
    //插入图片
    _addImage: function (event) {
      let _this = this;
      wx.chooseMedia({
        count: 1,
        mediaType: ['mix'],
        sourceType: ['album','camera'],
        maxDuration:10,
        success: function (res) {
         console.log(res);
          _this._uploadImage(res.tempFiles);
    
        },
        
      });
    },
    _uploadImage: function (tempFilePath) {
      let url = tempFilePath[0].tempFilePath
      wx.cloud.uploadFile({
        cloudPath: `${new Date().getTime()}.png`,
        filePath: url
      }).then(res => {
        // console.log(res.fileID);
        this.triggerEvent('passImgUrl',res.fileID);  //编辑器上传的图片传到上面
        let fileList = this.data.fileList
        fileList.push({ url:res.fileID})
        this.setData({
          fileList:fileList
        })
      })
    },
    //设置斜体
    _addItalic: function (e) {
      this.setData({
        addItalic_select: !this.data.addItalic_select
      })
      this.editorCtx.format("italic")
    },
    //添加粗体样式
    _addBold: function () {
      this.setData({
        addBold_select: !this.data.addBold_select
      })
      this.editorCtx.format("bold")
    },
    //设置标题
    _addHeader: function (e) {
      let headerType = e.currentTarget.dataset.header;
      this.editorCtx.format("header", headerType)
    },
    //设置文字的排列方式
    _addAlign: function (e) {
      let alignType = e.currentTarget.dataset.align;
      this.editorCtx.format("align", alignType);
    },
    //设置列表
    _addList: function (e) {
      let listType = e.currentTarget.dataset.list;
      this.editorCtx.format("list", listType);
    },
    //撤销
    _undo: function () {
      this.editorCtx.undo();
    },
    //监控输入
    _onInputting: function (e) {
      let html = e.detail.html;
      let text = e.detail.text;
      this.triggerEvent("input", { html: html, text: text }, {});
    }
  }
})
